package com.example.s06blog.contoller;

import com.example.s06blog.pojo.User;
import com.example.s06blog.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @auther Qiang
 * @date 2022/4/1 16:52
 */

@Controller
@RequestMapping("/user")
public class UserController
{
    @Autowired
    private IUserService userService;

    @GetMapping("/")
    public String index()
    {
        return "user/index";
    }

    @GetMapping("/login")
    public String login()
    {
        return "user/login";
    }

    @GetMapping("/loginchk")
    public String loginchk(String username, String password, HttpSession session)
    {
        // 1.

        // 2.
        User u = userService.login(username,password);
        System.out.println("user:"+u);

        // 3
        if(u!=null)
        {
            session.setAttribute("user",u);
            return "redirect:/user/";
        }
        else
        {
            // 跳回登录页
            return "redirect:/user/login";
        }

    }
}
